//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BlackCallableZeroCouponBondEngine.h"
using namespace Cephei::QL::Experimental::Callablebonds;
#include <gen/QL/Experimental/Callablebonds/CallableBondVolatilityStructure.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Experimental/Callablebonds/BlackCallableFixedRateBondEngine.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::CBlackCallableZeroCouponBondEngine (Cephei::QL::Experimental::Callablebonds::ICallableBondVolatilityStructure^ yieldVolStructure, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve) : CBlackCallableFixedRateBondEngine(CBlackCallableZeroCouponBondEngine::typeid)
{
    CCallableBondVolatilityStructure^ _CyieldVolStructure;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
#ifdef HANDLE
        _phBlackCallableZeroCouponBondEngine = NULL;
#endif
        _CyieldVolStructure = safe_cast<CCallableBondVolatilityStructure^> (yieldVolStructure);
        _CyieldVolStructure->Lock();
        Handle<QuantLib::CallableBondVolatilityStructure>& _yieldVolStructure = static_cast<Handle<QuantLib::CallableBondVolatilityStructure>&> (_CyieldVolStructure->GetHandle ()); 
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        _ppBlackCallableZeroCouponBondEngine = new boost::shared_ptr<QuantLib::BlackCallableZeroCouponBondEngine> (new QuantLib::BlackCallableZeroCouponBondEngine ( _yieldVolStructure,  _discountCurve ));
        SetBlackCallableFixedRateBondEngine (boost::dynamic_pointer_cast<QuantLib::BlackCallableFixedRateBondEngine> (*_ppBlackCallableZeroCouponBondEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CyieldVolStructure != nullptr) _CyieldVolStructure->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::CBlackCallableZeroCouponBondEngine (Cephei::QL::IQuote^ fwdYieldVol, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve) : CBlackCallableFixedRateBondEngine(CBlackCallableZeroCouponBondEngine::typeid)
{
    CQuote^ _CfwdYieldVol;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
#ifdef HANDLE
        _phBlackCallableZeroCouponBondEngine = NULL;
#endif
        _CfwdYieldVol = safe_cast<CQuote^> (fwdYieldVol);
        _CfwdYieldVol->Lock();
        Handle<QuantLib::Quote>& _fwdYieldVol = static_cast<Handle<QuantLib::Quote>&> (_CfwdYieldVol->GetHandle ()); 
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        _ppBlackCallableZeroCouponBondEngine = new boost::shared_ptr<QuantLib::BlackCallableZeroCouponBondEngine> (new QuantLib::BlackCallableZeroCouponBondEngine ( _fwdYieldVol,  _discountCurve ));
        SetBlackCallableFixedRateBondEngine (boost::dynamic_pointer_cast<QuantLib::BlackCallableFixedRateBondEngine> (*_ppBlackCallableZeroCouponBondEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfwdYieldVol != nullptr) _CfwdYieldVol->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::CBlackCallableZeroCouponBondEngine (boost::shared_ptr<QuantLib::BlackCallableZeroCouponBondEngine>& childNative, Object^ owner) : CBlackCallableFixedRateBondEngine(CBlackCallableZeroCouponBondEngine::typeid)
{
#ifdef HANDLE
	_phBlackCallableZeroCouponBondEngine = NULL;
#endif
	_ppBlackCallableZeroCouponBondEngine = &childNative;
    _ppBlackCallableFixedRateBondEngine = new boost::shared_ptr<QuantLib::BlackCallableFixedRateBondEngine> (boost::dynamic_pointer_cast<QuantLib::BlackCallableFixedRateBondEngine> (*_ppBlackCallableZeroCouponBondEngine));
}
Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::CBlackCallableZeroCouponBondEngine (QuantLib::BlackCallableZeroCouponBondEngine& childNative, Object^ owner) : CBlackCallableFixedRateBondEngine(CBlackCallableZeroCouponBondEngine::typeid)
{
#ifdef HANDLE
	_phBlackCallableZeroCouponBondEngine = NULL;
#endif
	_ppBlackCallableZeroCouponBondEngine = new boost::shared_ptr<QuantLib::BlackCallableZeroCouponBondEngine> (&childNative);
    _ppBlackCallableFixedRateBondEngine = new boost::shared_ptr<QuantLib::BlackCallableFixedRateBondEngine> (boost::dynamic_pointer_cast<QuantLib::BlackCallableFixedRateBondEngine> (*_ppBlackCallableZeroCouponBondEngine));
    _BlackCallableZeroCouponBondEngineOwner = owner;
    _BlackCallableFixedRateBondEngineOwner = owner;
}

Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::CBlackCallableZeroCouponBondEngine (CBlackCallableZeroCouponBondEngine^ copy) : CBlackCallableFixedRateBondEngine(CBlackCallableZeroCouponBondEngine::typeid)
{
#ifdef HANDLE
	_phBlackCallableZeroCouponBondEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackCallableZeroCouponBondEngine = new boost::shared_ptr<QuantLib::BlackCallableZeroCouponBondEngine> (copy->GetShared());
        _ppBlackCallableFixedRateBondEngine = new boost::shared_ptr<QuantLib::BlackCallableFixedRateBondEngine> (boost::dynamic_pointer_cast<QuantLib::BlackCallableFixedRateBondEngine> (*_ppBlackCallableZeroCouponBondEngine));
    }
}
Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::CBlackCallableZeroCouponBondEngine (PLATFORM::Type^ t) : CBlackCallableFixedRateBondEngine(CBlackCallableZeroCouponBondEngine::typeid)
{
#ifdef HANDLE
	_phBlackCallableZeroCouponBondEngine = NULL;
#endif
	if (!t->IsSubclassOf(CBlackCallableZeroCouponBondEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::CBlackCallableZeroCouponBondEngine (QuantLib::Handle<QuantLib::BlackCallableZeroCouponBondEngine>& childNative, Object^ owner)  : CBlackCallableFixedRateBondEngine(CBlackCallableZeroCouponBondEngine::typeid)
{
	_phBlackCallableZeroCouponBondEngine = &childNative;
	_ppBlackCallableZeroCouponBondEngine = &static_cast<boost::shared_ptr<QuantLib::BlackCallableZeroCouponBondEngine>>(childNative.currentLink());
    _ppBlackCallableFixedRateBondEngine = new boost::shared_ptr<QuantLib::BlackCallableFixedRateBondEngine> (boost::dynamic_pointer_cast<QuantLib::BlackCallableFixedRateBondEngine> (*_ppBlackCallableZeroCouponBondEngine));
    _BlackCallableZeroCouponBondEngineOwner = owner;
}
Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::CBlackCallableZeroCouponBondEngine (QuantLib::Handle<QuantLib::BlackCallableZeroCouponBondEngine> childNative)  : CBlackCallableFixedRateBondEngine(CBlackCallableZeroCouponBondEngine::typeid)
{
	_phBlackCallableZeroCouponBondEngine = &childNative;
	_ppBlackCallableZeroCouponBondEngine = &static_cast<boost::shared_ptr<QuantLib::BlackCallableZeroCouponBondEngine>>(childNative.currentLink());
    _ppBlackCallableFixedRateBondEngine = new boost::shared_ptr<QuantLib::BlackCallableFixedRateBondEngine> (boost::dynamic_pointer_cast<QuantLib::BlackCallableFixedRateBondEngine> (*_ppBlackCallableZeroCouponBondEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::CBlackCallableZeroCouponBondEngine (QuantLib::BlackCallableZeroCouponBondEngine childNative)  : CBlackCallableFixedRateBondEngine(CBlackCallableZeroCouponBondEngine::typeid)
{
#ifdef HANDLE
	_phBlackCallableZeroCouponBondEngine = NULL;
#endif
	_ppBlackCallableZeroCouponBondEngine = new boost::shared_ptr<QuantLib::BlackCallableZeroCouponBondEngine> (new QuantLib::BlackCallableZeroCouponBondEngine (childNative));
    _ppBlackCallableFixedRateBondEngine = new boost::shared_ptr<QuantLib::BlackCallableFixedRateBondEngine> (boost::dynamic_pointer_cast<QuantLib::BlackCallableFixedRateBondEngine> (*_ppBlackCallableZeroCouponBondEngine));
}
#endif

Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::~CBlackCallableZeroCouponBondEngine ()
{
    if (_ppBlackCallableZeroCouponBondEngine != NULL)
    {
	    delete _ppBlackCallableZeroCouponBondEngine;
        _ppBlackCallableZeroCouponBondEngine = NULL;
    }
}
Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::!CBlackCallableZeroCouponBondEngine ()
{
    if (_ppBlackCallableZeroCouponBondEngine != NULL)
    {
	    delete _ppBlackCallableZeroCouponBondEngine;
    }
}
QuantLib::BlackCallableZeroCouponBondEngine& Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::GetReference ()
{
    if (_ppBlackCallableZeroCouponBondEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppBlackCallableZeroCouponBondEngine;
}
boost::shared_ptr<QuantLib::BlackCallableZeroCouponBondEngine>& Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::GetShared ()
{
    if (_ppBlackCallableZeroCouponBondEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppBlackCallableZeroCouponBondEngine;
}
QuantLib::BlackCallableZeroCouponBondEngine* Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::GetPointer ()
{
    if (_ppBlackCallableZeroCouponBondEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppBlackCallableZeroCouponBondEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackCallableZeroCouponBondEngine>& Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::GetHandle ()
{
	if (_phBlackCallableZeroCouponBondEngine == NULL)
	{
		_phBlackCallableZeroCouponBondEngine = new Handle<QuantLib::BlackCallableZeroCouponBondEngine> (*_ppBlackCallableZeroCouponBondEngine);
	}
	return *_phBlackCallableZeroCouponBondEngine;
}
#endif
bool Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine::HasNative () 
{
	return (_ppBlackCallableZeroCouponBondEngine != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Callablebonds::IBlackCallableZeroCouponBondEngine^ Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine_Factory::Create (Cephei::QL::Experimental::Callablebonds::ICallableBondVolatilityStructure^ yieldVolStructure, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve)
{
    return REFNEW CBlackCallableZeroCouponBondEngine ( yieldVolStructure,  discountCurve);
}
Cephei::QL::Experimental::Callablebonds::IBlackCallableZeroCouponBondEngine^ Cephei::QL::Experimental::Callablebonds::CBlackCallableZeroCouponBondEngine_Factory::Create (Cephei::QL::IQuote^ fwdYieldVol, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve)
{
    return REFNEW CBlackCallableZeroCouponBondEngine ( fwdYieldVol,  discountCurve);
}
